package com.example.myfirstapplication

import android.content.Intent
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.text.Html
import android.text.Spanned
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.bumptech.glide.Glide
import com.example.myfirstapplication.utils.OkHttpUtils.Companion.mOkHttpUtils
import com.example.myfirstapplication.data.IP_Data
import com.example.myfirstapplication.utils.*
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_common_widget.*
import okhttp3.Response
import okio.IOException

class CommonWidget : BaseActivity() {
    private lateinit var ipInfo: IP_Data
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_common_widget)

       var htmlCode = "<font color='red'>红色文字</font><br>"
        htmlCode += "<font size='15'>15号文字</font>"
        val htmlText: Spanned =
            if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.N) {
                Html.fromHtml(htmlCode, Html.FROM_HTML_MODE_LEGACY)
            } else {
                Html.fromHtml(htmlCode)
            }
        textView_cw.text = htmlText

        val imgUrl = "http://www.jci.edu.cn/images/P120200410.jpg"
        btn_getImg.setOnClickListener {
            Glide.with(this).load(imgUrl).into(imageView)
        }

        //读取IP数据判断
        btn_getData.setOnClickListener {
            val json = FileOP().read(this@CommonWidget, "ip.json")
            Log.i(Tag, json)
            if (json.isNotEmpty()) {
                getIPDATAFromFile(json!!)
            } else {
                getIPData()
            }
            progressBar.visibility = View.INVISIBLE
        }

        btn_takephoto.setOnClickListener {
            try {
                val i = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                startActivityForResult(i, 99)
            } catch (e: Exception) {
                Log.d(Tag, e.toString())
            }
        }
        var eduVal = ""
        val eduArr = arrayOf("", "幼儿园", "小学", "初中", "中专", "高中", "大专", "本科", "研究生")
        val spinnerAdapter =
            ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, eduArr)
        spinner_edu.adapter = spinnerAdapter
        spinner_edu.prompt = "请选择"
        spinner_edu.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {
                TODO("Not yet implemented")
            }

            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                if (position != 0) {
                    eduVal = eduArr[position]
                    myToast(
                        this@CommonWidget,
                        eduVal
                    )
                } else {
                    myToast(
                        this@CommonWidget,
                        "请做选择"
                    )
                }
            }
        }
        val autoArr = arrayOf("张三", "张三丰", "张无忌", "张果老", "张国荣")
        val autoTxtAdapter =
            ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, autoArr)
        autoTxtView.setAdapter(autoTxtAdapter)
        autoTxtView.threshold = 1
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        val revData = data?.extras
        when (requestCode) {
            99 -> {
                val pic = revData?.get("data") as Bitmap
                imageView.setImageBitmap(pic)
            }
        }
    }

    //从文件读取IP
    private fun getIPDATAFromFile(json: String) {
        ipInfo = Gson().fromJson(json!!, IP_Data::class.java)
        Log.i(Tag, "国家：" + ipInfo.data.ref_2.country)
        Log.i(Tag, "省份：" + ipInfo.data.ref_2.province)
        Log.i(Tag, "城市：" + ipInfo.data.ref_2.city)
        var outInfo = "你所在的国家：" + ipInfo.data.ref_2.country
        outInfo += "\n你所在的省份：" + ipInfo.data.ref_2.province
        outInfo += "\n你所在的城市：" + ipInfo.data.ref_2.city
        textView_cw.text = outInfo
        myToast(this, "ip data from file")
    }

    //从互联网读取IP
    private fun getIPData() {
        //初始化Okhttp环境
        OkHttpUtils(this)
        val url = "http://widget.cifuwu.com/ip"
        mOkHttpUtils.get_Async(url, object : OkHttpUtils.OkHttpCallback {
            override fun onResponse(response: Response) {
                Log.i(Tag, response.toString())
                if (response.isSuccessful) {
                    //    Log.i(Tag,response.body?.string())
                    val json = response.body!!.string()
                    ipInfo = Gson().fromJson(json, IP_Data::class.java)
                    Log.i(Tag, "国家：" + ipInfo.data.ref_2.country)
                    Log.i(Tag, "省份：" + ipInfo.data.ref_2.province)
                    Log.i(Tag, "城市：" + ipInfo.data.ref_2.city)
                    var outInfo = "你所在的国家：" + ipInfo.data.ref_2.country
                    outInfo += "\n你所在的省份：" + ipInfo.data.ref_2.province
                    outInfo += "\n你所在的城市：" + ipInfo.data.ref_2.city

                    FileOP().save(this@CommonWidget, "ip.json", json!!)
                    textView_cw.text = outInfo
                    myToast(this@CommonWidget, "ip data from internet")
                }
                progressBar.visibility = View.GONE
            }

            override fun onError(e: IOException) {
                Log.i(Tag, e.toString())
                progressBar.visibility = View.GONE
            }
        })
    }
}
